package cn.demon.org.controller;


import cn.demon.basic.exception.BusinessException;
import cn.demon.basic.util.AjaxResult;
import cn.demon.basic.util.ExcelUtil;
import cn.demon.basic.util.PageList;
import cn.demon.dto.ShopDto;
import cn.demon.org.domain.Shop;
import cn.demon.org.domain.ShopAuditLog;
import cn.demon.org.query.ShopQuery;
import cn.demon.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 接口类：店铺
 */
@RestController
@RequestMapping("/shop")
@Api(value = "店铺接口",description = "店铺接口")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 接口：查询单个对象
     * @param id
     * @return
     */
    //http://localhost:8080/shop/1 + get
    @ApiOperation(value = "查询单个对象")
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id")Long id){
        return shopService.findById(id);
    }

    /**
     * 接口：查询所有对象
     * @return
     */
    //http://localhost:8080/shop + 无 + get
    @ApiOperation(value = "查询所有对象")
    @GetMapping
    public List<Shop> findAll(){
        return shopService.findAll();
    }

    /**
     * 接口：分页查询
     * @param query
     * @return
     */
    //http://localhost:8080/shop + {json对象} + post
    @ApiOperation(value = "分页查询")
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        return shopService.queryPage(query);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    //http://localhost:8080/shop/1 + delete
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     * 接口：添加或修改
     * @param shop
     * @return
     */
    @ApiOperation(value = "添加或修改")
    //http://localhost:8080/shop + {json对象} + put
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId() == null){//添加
                shopService.add(shop);
            }else{
                shopService.update(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }



    /**
     * 接口：批量删除
     * @param
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            shopService.patchDelete(ids);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }
    //商家入驻接口 //先写主体，再写细节
    @ApiOperation(value = "商家入驻",notes = "")
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e){ //业务异常捕获
            return AjaxResult.me().setSuccess(false).setMessage("入驻失败!"+e.getMessage());
        } catch (Exception e) { //500
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常,请稍后重试!");
        }
    }

    /**
     * 接口：店铺审核通过
     * @param
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "店铺审核通过")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"审核失败");
        }
    }

    /**
     * 接口：驳回店铺入驻
     * @param
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "店铺审核驳回")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"驳回失败");
        }
    }


    /**
     * 文件导出Excel
     * @param response
     */
    @GetMapping("/export")
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.findAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件导入
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        //String file = "F:\\文件名.xls";
        //解析excel，
        List<Shop> shops = ExcelUtil.importExcel(file,0,1,Shop.class);
        shops.forEach(System.out::println);
    }
    @GetMapping("/echarts")
    public List<ShopDto> getShopState(){
        return shopService.getShopState();
    }
}